package com.qupeng.demo.kafka.kafkaapache.producer;

import org.apache.kafka.common.serialization.Serializer;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

public class CustomizedSerializer implements Serializer<Product> {
    @Override
    public byte[] serialize(String topic, Product product) {
        byte[] name = product.getName().toString().getBytes(StandardCharsets.UTF_8);
        ByteBuffer buffer = ByteBuffer.allocate(4 + 4 + name.length);
        buffer.putInt(product.getId());
        buffer.putInt(name.length);
        buffer.put(name);
        return buffer.array();
    }
}
